jQuery(document).ready(function($) {

    //头部轮播
    $('#J_top_slides').slide({
        mainCell: '.bd ul',
        autoPlay: true,
        effect: 'fold'
    });

    //网站通知轮播
    $('#J_notice_slides').slide({
        mainCell: '.bd ul',
        autoPlay: true,
        effect: 'left'
    });

    //左侧快速导航 Start
    var $window = $(window);
    var quickNav = $('#J_quick_nav'); //快速导航
    var sections = []; //所有板块
    var quickLinks = quickNav.find('a'); //所有快速导航链接

    quickLinks.each(function(index, el) {
        var sectionId = $(el).data('target');
        sections.push($(sectionId));
    });

    function refreshQuickNav() {
        var scrollHeight = $window.scrollTop();
        var curSecIdx = -1;     //当前板块序号
        var top;    //板块距浏览器顶部距离

        //找到当前板块
        for (var k = 0; k < sections.length; k++) {
            top = sections[k].offset().top - scrollHeight;
            if (k === 0) {
                if (top < 160) {
                    curSecIdx ++;
                }
            } else {
                if (top < 300) {
                    curSecIdx ++;
                }
            }
        }

        var prevActiveLink = quickNav.find('.current');
        if (prevActiveLink.length) {
            prevActiveLink.removeClass('current');
        }
        if (curSecIdx === -1) {
            quickNav.hide();
        } else {
            if (!quickNav.is(':visible')) {
                quickNav.show();
            }
            quickLinks.eq(curSecIdx).closest('li').addClass('current');
        }
    }

    $window.on('scroll', refreshQuickNav);
    $window.on('load', refreshQuickNav);

    //点击定位板块
    quickNav.on('click', 'a', function(event) {
        var secId = $(this).data('target');
        $(secId)[0].scrollIntoView();
    });
    //左侧快速导航 End
});